/**
 * 
 */
package heterotopia.oa;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

/**
 * @author garrett
 */
public class DurableObjectDaoImpl implements DurableObjectDao {
	@PersistenceContext
	protected EntityManager entityManager;

	public DurableObjectDaoImpl() {
	}

	/* (non-Javadoc)
	 * @see heterotopia.oa.DurableObjectDao#find(java.util.UUID)
	 */
	public DurableObject find(String id) {
		return entityManager.find(DurableObject.class, id);
	}

	/* (non-Javadoc)
	 * @see heterotopia.oa.DurableObjectDao#save(heterotopia.oa.DurableObject)
	 */
	@Transactional
	public void persist(DurableObject o) {
		if(entityManager.contains(o))
			entityManager.merge(o);
		else
			entityManager.persist(o);
	}

	/* (non-Javadoc)
	 * @see heterotopia.oa.DurableObjectDao#delete(heterotopia.oa.DurableObject)
	 */
	@Transactional
	public void remove(DurableObject o) {
		entityManager.remove(o);
	}

	@SuppressWarnings("unchecked")
	public Collection<DurableObject> list() {
		Query query = entityManager.createQuery("SELECT e FROM DurableObject e");
	    return (Collection<DurableObject>) query.getResultList();
	}
}
